<?php

require_once '../config.php';
require_once '../functions.php';

books_get_current_user();



function add_category()
{
    if (empty($_POST['name']) || empty($_POST['QQ'])) {
        $GLOBALS['message'] = '请完整填写表单！';
        $GLOBALS['success'] = false;
        return;
    }

    // 接收并保存
    $name = $_POST['name'];
    $QQ = $_POST['QQ'];
    $nick = $_POST['nick'];
    $ISBN = $_POST['ISBN'];
    $data = $_POST['data'];
    $content = $_POST['content'];


    // insert into categories values (null, 'slug', 'name');
    $rows = book_execute("insert into make values (null, '{$name}', '{$QQ}','{$nick}','{$ISBN}','{$data}','{$content}');");

    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['message'] = $rows <= 0 ? '添加失败！' : '添加成功！';
}

function edit_category()
{
    global $current_edit_category;


    $id = $current_edit_category['id'];
    $name = empty($_POST['name']) ? $current_edit_category['name'] : $_POST['name'];
    $current_edit_category['name'] = $name;
    $QQ = empty($_POST['QQ']) ? $current_edit_category['make_qq'] : $_POST['QQ'];
    // 同步数据
    $current_edit_category['QQ'] = $QQ;
    $nick = empty($_POST['nick']) ? $current_edit_category['make_nick'] : $_POST['nick'];
    $current_edit_category['make_nick'] = $nick;

    $ISBN = empty($_POST['ISBN']) ? $current_edit_category['make_isbn'] : $_POST['ISBN'];
    $current_edit_category['make_isbn'] = $ISBN;

    $data = empty($_POST['data']) ? list_date($current_edit_category['make_data']) : $_POST['data'];
    $current_edit_category['make_data'] = $data;
  
    $content = empty($_POST['content']) ? list_date($current_edit_category['make_content']) : $_POST['content'];
    $current_edit_category['make_content'] = $content;

 


    // insert into categories values (null, 'slug', 'name');
    $rows = book_execute("update make set name = '{$name}', make_nick = '{$nick}', make_qq = '{$QQ}', make_isbn = '{$ISBN}',make_data='{$data}' ,make_content = '{$content}' where id = {$id}");

    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['message'] = $rows <= 0 ? '更新失败！' : '更新成功！';
}

if (empty($_GET['id'])) {

  // 添加
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        add_category();
    }
} else {
    $current_edit_category = book_fetch_one('select * from make where id = ' . $_GET['id']);
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        edit_category();
    }
}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Categories &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
  <?php include 'inc/navbar_admin.php'; ?>

    <div class="container-fluid">
      <div class="page-title">
      </div>
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
      <?php if ($success): ?>
      <div class="alert alert-success">
        <strong>成功！</strong> <?php echo $message; ?>
      </div>
      <?php else: ?>
      <div class="alert alert-danger">
        <strong>错误！</strong> <?php echo $message; ?>
      </div>
      <?php endif ?>
      <?php endif ?>
      <div class="row">
        <div class="col-md-4">
          <?php if (isset($current_edit_category)): ?>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $current_edit_category['id']?>" method="post">
            <h2>编辑   <?php echo $current_edit_category['make_qq']; ?></h2>
            <div class="form-group">
              <label for="name">名称</label>
              <input id="name" class="form-control" name="name" type="text" placeholder="分类名称" value="<?php echo $current_edit_category['name']; ?>">
            </div>
            <div class="form-group">
              <label for="QQ">邮箱</label>
              <input id="QQ" class="form-control" name="QQ" type="text" placeholder="QQ" value="<?php echo $current_edit_category['make_qq']; ?>">
            </div>
            <div class="form-group">
              <label for="nick">昵称</label>
              <input id="nick" class="form-control" name="nick" type="text" placeholder="nick" value="<?php echo $current_edit_category['make_nick']; ?>">
            </div>
            <div class="form-group">
              <label for="ISBN">ISBN</label>
              <input id="ISBN" class="form-control" name="ISBN" type="text" placeholder="ISBN" value="<?php echo $current_edit_category['make_isbn']; ?>">
            </div>
            <div class="form-group">
              <label for="data">日期</label>
              <input id="data" class="form-control" name="data" type="date" placeholder="data"><?php echo $current_edit_category['make_data']; ?>
            </div>
           
            <div class="form-group">
              <label for="content">句子</label>
              <!-- <input id="content" class="form-control" name="content" type="text" placeholder="content" value=""> -->
              <textarea class="form-control" name="content" id="content" cols="30" rows="3"><?php echo $current_edit_category['make_content']; ?></textarea>
            </div>
           



            <div class="form-group">
              <button class="btn btn-primary" type="submit">保存</button>
              <a href="/admin/minute.php" class="btn btn-primary" type="submit">返回</a>
            </div>
            
          </form>
          <?php else: ?>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <h2>添加新分类目录</h2>
            <div class="form-group">
              <label for="name">名称</label>
              <input id="name" class="form-control" name="name" type="text" placeholder="分类名称" >
            </div>
            <div class="form-group">
              <label for="QQ">邮箱</label>
              <input id="QQ" class="form-control" name="QQ" type="text" placeholder="QQ" >
            </div>
            <div class="form-group">
              <label for="nick">昵称</label>
              <input id="nick" class="form-control" name="nick" type="text" placeholder="nick">
            </div>
            <div class="form-group">
              <label for="ISBN">密码</label>
              <input id="ISBN" class="form-control" name="ISBN" type="text" placeholder="ISBN" >
            </div>
            <div class="form-group">
              <label for="data">日期</label>
              <input id="data" class="form-control" name="data" type="date" placeholder="data">
            </div>
            <div class="form-group">
              <label for="content">句子</label>
              <input id="content" class="form-control" name="content" type="text" placeholder="content">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
              <a href="/admin/minute.php" class="btn btn-primary" type="submit">返回</a>
            </div>
          </form>
          <?php endif ?>
        </div>
       
      </div>
    </div>
  </div>

  <?php include 'inc/sidebar.php'; ?>
 


  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>
</body>
</html>
